﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Areas.Management.Controllers
{
    public class ArchiveTypeListController : Controller
    {
        // Create an instance of user manage
        LeadVinaglue.Repository.ArchiveTypeManagementRepository repository = new LeadVinaglue.Repository.ArchiveTypeManagementRepository();

        //
        // GET: /Management/ArchiveTypeList/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            ArchiveTypeManagementModel model = new ArchiveTypeManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 0);

            return View(model);
        }

        //
        // GET: /Management/ArchiveTypeList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add()
        {
            ArchiveTypeManagementModel model = new ArchiveTypeManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 0);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add(FormCollection form)
        {
            ArchiveType model = new ArchiveType();

            model.ModuleName = form["ModuleName"].ToString();
            model.Name = form["Name"].ToString();
            int isInt;
            if (form["SortOrder"] != null && !string.IsNullOrEmpty(form["SortOrder"].ToString()) && int.TryParse(form["SortOrder"].ToString(), out isInt))
                model.SortOrder = int.Parse(form["SortOrder"].ToString());
            else
                model.SortOrder = 0;
            model.ParentID = 0;
            model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

            repository.Create(model);

            return RedirectToAction("Index", "ArchiveTypeList");
        }

        //
        // GET: /Management/ArchiveTypeList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            ArchiveTypeManagementModel model = new ArchiveTypeManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.ArchiveTypeId = id;

            return View(model);
        }

        //
        // GET: /Management/ArchiveTypeList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            ArchiveType model = new ArchiveType();

            if (model != null)
            {
                model.ID = id;
                model.ModuleName = form["ModuleName"].ToString();
                model.Name = form["Name"].ToString();
                int isInt;
                if (form["SortOrder"] != null && !string.IsNullOrEmpty(form["SortOrder"].ToString()) && int.TryParse(form["SortOrder"].ToString(), out isInt))
                    model.SortOrder = int.Parse(form["SortOrder"].ToString());
                else
                    model.SortOrder = 0;

                model.ParentID = 0;
                model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

                repository.Save(model);
            }

            return RedirectToAction("Index", "ArchiveTypeList");
        }

        // Delete Email Item
        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Delete(int id)
        {
            repository.Delete(id);

            ArchiveTypeManagementModel model = new ArchiveTypeManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }
}
